﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DarwinIsDead.Traits {
    /// <summary>
    /// Shoots directly at player
    /// </summary>
    class StraightShooter : Trait {
        private Player player;
        private Random rng;

        public List<Bullet> Bullets { get; private set; }
        public Texture2D Sprite { get; set; }
        public Vector2 SpriteAxis { get; set; }
        public Vector2 Size { get; private set; }
        public int Shots { get; private set; }
        public float FireDelay { get; private set; }
        public float BurstDelay { get; private set; }
        public int HealthBonus { get; set; }
        public float PowerGrowth { get; set; }
        
        private float powerModifier;
        private float fireCount;
        private float burstCount;
        private int shotCount;
        private int currentBullet;

        public StraightShooter(World world, Vector2 size, int shots, float fireDelay, float burstDelay) :
            base(null, world) {
            Bullets = new List<Bullet>();
            Sprite = new Texture2D(world.Game.GraphicsDevice, (int)(size.X + 0.5), (int)(size.Y + 0.5));
            rng = new Random();

            Color[] data = new Color[(int)(size.X * size.Y + 0.5)];
            Color col = new Color(rng.Next(255), rng.Next(255), rng.Next(255));
            for (int i = 0; i < data.Length; i++) {
                data[i] = col;
            }

            Sprite.SetData(data);
            SpriteAxis = new Vector2();
            Size = size;
            Shots = shots;
            FireDelay = fireDelay;
            BurstDelay = burstDelay;
            PowerGrowth = 1f;
            currentBullet = 0;
        }

        public override object Clone() {
            StraightShooter ret = new StraightShooter(World, Size, Shots, FireDelay, BurstDelay);
            ret.Sprite = Sprite;
            ret.SpriteAxis = new Vector2(SpriteAxis.X, SpriteAxis.Y);
            ret.Bullets = new List<Bullet>(Bullets);
            ret.HealthBonus = HealthBonus;
            ret.PowerGrowth = PowerGrowth;
            return ret;
        }

        public override void Initialize() {
            List<Entity> players = World.GetEntities<Player>();
            if (players.Count > 0) {
                player = (Player)players[0];
            }

            Owner.MaxHealth += HealthBonus;
            fireCount = 0f;
            Owner.HitBox.Width += (int)Size.X;
            Owner.HitBox.Height += (int)Size.Y;
        }

        protected override void AdjustLevel(int level) {
            powerModifier = PowerGrowth * level;
        }

        protected override void InternalUpdate(GameTime gameTime) {
            float dt = (float)(gameTime.ElapsedGameTime.Milliseconds / 1000f);

            burstCount += dt;
            if (burstCount > BurstDelay) {
                fireCount += dt;
                if (fireCount > FireDelay) {
                    fireCount = 0f;
                    shotCount++;
                    if (shotCount < Shots && Bullets.Count > 0) {
                        Bullet bullet = Bullets[currentBullet];
                        currentBullet = (currentBullet + 1) % Bullets.Count;

                        float dx = player.Position.X - Owner.Position.X;
                        float dy = player.Position.Y - Owner.Position.Y;

                        float dir = (float)Math.Atan2(dy, dx);

                        bullet.Position = Owner.Position;
                        bullet.FireAngle = dir;
                        bullet.Active = true;
                        bullet.Solid = true;
                    } else {
                        shotCount = 0;
                        burstCount = 0f;
                    }
                }
            }
        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color) {
            Vector2 pos = new Vector2(Owner.Position.X + SpriteAxis.X,
                                      Owner.Position.Y + SpriteAxis.Y);
            //spriteBatch.Draw(Sprite, pos, Color.White);
            spriteBatch.Draw(Sprite, pos, null, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f);
        }
    }
}
